package com.tfswx.jcyw.sqlmodel.gic.operators;

import com.alibaba.fastjson.JSON;
import com.tfswx.jcyw.sqlmodel.api.Operator;
import com.tfswx.jcyw.sqlmodel.api.bean.Node;
import com.tfswx.jcyw.sqlmodel.api.bean.jdpz.InputJdpz;
import com.tfswx.jcyw.sqlmodel.api.bean.jdpz.ZdItem;
import com.tfswx.jcyw.sqlmodel.api.bean.table.*;
import com.tfswx.jcyw.sqlmodel.api.bean.table.expression.Column;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张述江
 * @date 2023/9/7
 */
public class DefaultInputOperator implements Operator {
    @Override
    public String getName() {
        return "输入";
    }


    @Override
    public Select operate(Node node, List<Select> pResultList) {
        InputJdpz jdpz = JSON.parseObject(node.getJdpz(), InputJdpz.class);
        InputJdpz.Sjypz sjypz = jdpz.getSjypz();

        System.out.println("执行了" + getName() + "操作");
        String sjylx = sjypz.getSjylx();
        if (!"数据库".equals(sjylx)) {
            throw new RuntimeException("输入操作只能用于数据库");
        }
        PlainSelect select = new PlainSelect(new TableFromItem(sjypz.getSjy()));
        List<SelectItem> selectItems = new ArrayList<>();
        for (ZdItem zdItem : jdpz.getZdList()) {
            selectItems.add(new SelectItem(new Column(zdItem.getZdbds().toString()), new Alias(zdItem.getZdbm())));
        }
        select.addSelectItem(selectItems);
        return select;
    }
}
